package com.yuyou.fn.timer.task;

import com.github.wens.mybatisplus.examples.Example;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.BaseGrade;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.service.IBaseGradeService;
import com.yuyou.fn.educational.service.IStudentService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;


@JobHander(value="ChangeStudentGradeTask")
@Component
public class ChangeStudentGradeTask extends IJobHandler{

    @Resource
    private IStudentService studentService ;

    @Resource
    private IBaseGradeService baseGradeService ;

    @Override
    public ReturnT<String> execute(String... params) throws Exception {

        Example<BaseGrade> gradeExample = BaseGrade.createExample();
        gradeExample.orderBy("gradeId");
        List<BaseGrade> gradeList = baseGradeService.findListByExample(gradeExample);

        Example<Student> studentExample = Student.createExample();
        studentExample.selectProperties("gradeId");

        List<Student> studentList = studentService.findListByExample(studentExample);

        for(Student student : studentList ){
            int gIdx  =  - 1 ;
            for(int i = 0 ; i < gradeList.size() ;i++ ){
                if(gradeList.get(i).getGradeId().equals(student.getGradeId())){
                    gIdx = i ;
                    break;
                }
            }

            gIdx++ ;

            if(gIdx < gradeList.size()){
                Student up = new Student() ;
                up.setStudentId(student.getStudentId());
                BaseGrade grade = gradeList.get(gIdx);
                up.setGradeName(grade.getGradeName());
                up.setGradeId(grade.getGradeId());
                up.setUpdateTime(TimeUtils.now());
                studentService.updateSelectiveById(up);

            }

        }
        return ReturnT.SUCCESS ;
    }

}
